﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;

using API.Models;
using API.Models.Repository;

namespace API.Controllers.api
{
    public class OutgoingInvoicesController : BasicController
    {
        ///[Queryable]
        public virtual IQueryable<V_OutgoingInvoices> Get()
        {
            var repository = new OutgoingInvoices_Repository();
            repository.SetSession(this.currentSession);

            var data = repository.Find().AsQueryable();
            return data;
        }

        public virtual V_OutgoingInvoices Get(Guid id)
        {
            var repository = new OutgoingInvoices_Repository();
            repository.SetSession(this.currentSession);

            var data = repository.Find(r => r.GUID_RECORD == id);
            return data.FirstOrDefault();
        }

        public void Post(V_OutgoingInvoices value)
        {
            var repository = new OutgoingInvoices_Repository();
            repository.SetSession(this.currentSession);

            if (value.GUID_RECORD == Guid.Empty)
                value.GUID_RECORD = Guid.NewGuid();

            repository.Edit(value);
            repository.Save();
            repository.Recode(value.GUID_RECORD);
        }

        public void Put(V_OutgoingInvoices value)
        {
            var repository = new OutgoingInvoices_Repository();
            repository.SetSession(this.currentSession);

            if (value.GUID_RECORD == Guid.Empty)
                value.GUID_RECORD = Guid.NewGuid();

            repository.Edit(value);
            repository.Save();
        }

        [HttpDelete]
        public virtual void Delete(Guid id)
        {
            var repository = new OutgoingInvoices_Repository();
            repository.SetSession(this.currentSession);

            var entity = repository.Find(r => r.GUID_RECORD == id).FirstOrDefault();
            repository.Delete(entity);
            repository.Save();
        }
    
    }
}
